package com.sndscl.rpc.common.rpc;

import com.alibaba.fastjson.JSONObject;
import com.sndscl.rpc.common.dto.RpcResponse;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.util.CharsetUtil;

/**
 * @Description
 * @Author lsh
 * @Date 2020/10/13 14:55
 **/
public class ResponseEncoder extends MessageToByteEncoder<RpcResponse> {

    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, RpcResponse rpcResponse, ByteBuf byteBuf) throws Exception {
        byte[] bytes = JSONObject.toJSONString(rpcResponse).getBytes(CharsetUtil.UTF_8);
        byteBuf.writeBytes(bytes);
    }
}
